<?php

namespace App\Http\Controllers\Wap;

use App\Model\Member;
use App\Model\Product;
use App\Model\Scenery;
use App\Model\SceneryRegion;
use App\Model\SceneryType;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Util\PageHelper;

class SceneryController extends Controller
{
    /**
     * @param Request $request
     * czs
     */
    public function __construct(Request $request){
        $this->request = $request;
        $this->limit = 6;
        $this->openid = session('wechat_user')['id'];
        if (!empty($this->openid)) {
            $this->memberId = Member::where('openid',$this->openid)->pluck('id');
        }
    }

    /**
     * @return $this
     * 景区列表
     * czs
     */
	public function index(){
        $pid = $this->request->input('pid');
        $rid = $this->request->input('id');
        $type = $this->request->input('type');
        $key = $this->request->input('key');//搜索
        $sel = Scenery::sceneryList($pid,$rid,$type,$key,$this->limit)->getData();
        $parent = Scenery::parentList()->getData();
        $city = Scenery::cPull($pid)->getData();
        $pName = Scenery::pPull($pid)->getData();
        $cName = Scenery::childPull($rid)->getData();
        $typeName = SceneryType::typePull($type)->getData();
        $type = SceneryType::typeEnum()->getData();
        return view('wap.scenery.index')
            ->with('parent',$parent)
            ->with('type',$type)
            ->with('sel',$sel)
            ->with('pName',$pName)
            ->with('city',$city)
            ->with('cName',$cName)
			->with('key',$key)
            ->with('typeName',$typeName);
    }
    public function pullajax() {
        $pid = $this->request->get('pid');
        $rid = $this->request->get('rid');
        $type = $this->request->get('type');
        $key = $this->request->get('key');
        $list = Scenery::sceneryList($pid,$rid,$type,$key,$this->limit)->getData();
        $html = view('wap.scenery.list-ajax')->with('sel',$list)->render();
        $data = [
            'html'=>$html,
            'more'=>$list->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * 景区详情
     * czs
     */
    public function detail(){
        $id = $this->request->get('id');
        $one = Scenery::sceneryDetail($id)->getData();
        $functionId = $one->function_id;
        $productId = Product::where('function_id',$functionId)
            ->where('site',2)
            ->where('status',1)
            ->orderBy('id','asc')
            ->limit(1)
            ->pluck('id');
        return view('wap.scenery.detail')
            ->with('one',$one)
            ->with('productId',!empty($productId)?$productId:"");
    }

    /**
     * 二级联动
     * czs
     */
    public function pull() {
        $pid = $this->request->get('pid');
        $res = Scenery::pull($pid);
        return $res->getData();
    }
}
